package com.mfs.common.utills;

import com.alibaba.fastjson2.JSON;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 11747
 * @date 2023-07-01
 * */
public class StringUtil {
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isBlank(String str) {
        return isEmpty(str) || str.matches("\\s*");
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static boolean isPhoneNo(String str) {
        return isNotBlank(str) && str.matches("(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}");
    }

    public static boolean isEmailAddress(String str) {
        return isNotBlank(str) && str.matches("([A-z0-9\\-]+)@[A-z0-9\\-]+\\.([A-z]{2,6})");
    }

    public static String getFirst(String source, String regexp) {
        if (source == null) {
            return null;
        }
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher = pattern.matcher(source);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

    /**
     * 整形数字判断
     * */
    public static boolean isInt(String str) {
        try {
            Integer.valueOf(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 数字类型判断
     * */
    public static boolean isNumber(String str) {
        if (isBlank(str)) {
            return false;
        }
        return str.matches("[\\-,\\+]?\\d+[\\.\\\\d+]?");
    }

    /**
     * 暂不支持基本数据类型，支持其封装类型
     * */
    public static <T> T cast(String s, Class<T> type) {
        if (type == boolean.class || type == Boolean.class) {
            return type.cast(!(isBlank(s) || s.equals("0") || s.toLowerCase().equals("false")));
        } else if (type == byte.class || type == Byte.class) {
            try {
                return type.cast(Short.valueOf(s));
            } catch (Exception e) {
                return null;
            }
        } else if (type == int.class || type == Integer.class) {
            try {
                return type.cast(Integer.valueOf(s));
            } catch (Exception e) {
                return null;
            }
        } else if (type == long.class || type == Long.class) {
            try {
                return type.cast(Long.valueOf(s));
            } catch (Exception e) {
                return null;
            }
        } else if (type == float.class || type == Float.TYPE) {
            try {
                return type.cast(Float.valueOf(s));
            } catch (Exception e) {
                return null;
            }
        } else if (type == double.class || type == Double.class) {
            try {
                return type.cast(Double.valueOf(s));
            } catch (Exception e) {
                return null;
            }
        } else if (type == char.class || type == Character.class) {
            return type.cast(s.length() > 0 ? s.charAt(0) : null);
        } else {
            try {
                return type.cast(JSON.parse(s));
            } catch (Exception e) {
                return null;
            }
        }
    }
}
